package com.zhinong.controller.user;

import com.zhinong.entity.Order;
import com.zhinong.result.Result;
import com.zhinong.service.CartService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/orders")
    public Result<List<Order>> getCartOrders() {
        List<Order> orders = cartService.getCartOrders();
        return Result.success(orders);
    }

    @PostMapping("/updateQuantity")
    public Result<Void> updateQuantity(
            @RequestParam String productId,
            @RequestParam Integer change) {
        cartService.updateQuantity(productId, change);
        return Result.success();
    }

    @PostMapping("/updateSelection")
    public Result<Void> updateSelection(
            @RequestParam String id,
            @RequestParam Boolean selected,
            @RequestParam(required = false) Boolean isStore) {
        cartService.updateSelection(id, selected, isStore);
        return Result.success();
    }

    @GetMapping("/total")
    public Result<Map<String, Object>> getCartTotal() {
        Map<String, Object> result = cartService.getCartTotal();
        return Result.success(result);
    }
}